package com.fxp.project.jiuselu.web.business.v1.controller.common;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fxp.project.jiuselu.web.base.controller.BaseController;
import com.fxp.project.jiuselu.web.business.v1.request.areas.ReqArea;
import com.fxp.project.jiuselu.web.domain.CommonResult;
import com.fxp.project.jiuselu.web.models.common.entity.Areas;
import com.fxp.project.jiuselu.web.models.common.service.IAreasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 行政区划数据 前端控制器
 * </p>
 *
 * @author Jason.Wang
 * @since 2021-12-05
 */
@RestController
@RequestMapping("/v1/common/areas")
public class AreasController extends BaseController {
    @Autowired
    private IAreasService areasService;

    /**
     * 查询行政区划数据列表
     */
    @PostMapping("/list")
    @CrossOrigin
    public CommonResult list(@RequestBody @Validated ReqArea reqArea, BindingResult result) {
        super.checkBindResult(result);
        QueryWrapper<Areas> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", reqArea.getId());
        List<Areas> list = areasService.list(queryWrapper);
        return CommonResult.success(list);
    }

    /**
     * 获取行政区划数据详细信息
     */
    @PostMapping(value = "/detail")
    @CrossOrigin
    public CommonResult getInfo(@RequestBody @Validated ReqArea reqArea, BindingResult result) {
        super.checkBindResult(result);
        return CommonResult.success(areasService.getById(reqArea.getId()));
    }
}
